package com.zmn.oms.admin.controller.conf.reply.category;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.conf.reply.ReplyCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 类描述:回访类别修改控制器
 *
 * @author linfeng
 * @since 2020/04/15/14:47
 */
@Controller
@RequestMapping(value = "/conf/reply/category/")
public class ReplyCategoryModifyController extends ReplyCategoryBaseController {
    private Logger logger = LoggerFactory.getLogger(ReplyCategoryModifyController.class);

    @RequestMapping(value = "updateStatus")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/category/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的回访类别！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        replyCategoryService.updateStatusBatch(arrayIds, status, getMcStaffRealName());

        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "updateSort")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/category/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的回访类别！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置回访类别排序值！");
        }

        replyCategoryService.updateSortKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "updateFirstLetter")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/category/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改首字母的回访类别！");
        }
        if (firstLetter.isEmpty()) {
            return ResponseDTO.fail("请设置首字母！");
        }

        replyCategoryService.updateFirstLetterByKey(id, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    @RequestMapping(value = "addEdit")
    @ResponseBody
    //@RequiresPermissions("/conf/reply/category/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, HttpServletResponse response,
        @RequestBody ReplyCategory replyCategory) {
        Date now = DateUtil.getNow();

        Integer categId = replyCategory.getCategId();

        try {
            if (categId == null) {
                replyCategory.setCreater(getMcStaffRealName());
                replyCategory.setCreateTime(now);
                replyCategory.setStatus(GlobalConsts.YES);
                if (replyCategory.getSort() == null) {
                    replyCategory.setSort(1);
                }
                if (replyCategory.getName() != null) {
                    replyCategory.setFirstLetter(InitialUtil.getFristEnName(replyCategory.getName()));
                }

                categId = replyCategoryService.insert(replyCategory);
            } else {
                ReplyCategory result = replyCategoryService.findById(categId);
                result.setSort(replyCategory.getSort());
                result.setName(replyCategory.getName());
                result.setUpdater(getMcStaffRealName());
                result.setUpdateTime(now);
                if (replyCategory.getName() != null) {
                    result.setFirstLetter(InitialUtil.getFristEnName(replyCategory.getName()));
                }
                replyCategoryService.updateReplyCategoryByKey(result);
            }

            return ResponseDTO.success(true,"操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("回访类别【{}】已经存在，不能重复添加！", replyCategory.getName());
            return ResponseDTO.fail("回访类别已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改回访类别失败,returnvisitcategory:{},ex:{}", replyCategory, ex.getMessage(), ex);
            return ResponseDTO.fail("操作失败！");
        }
    }
}
